package com.fi.common.log;

import fivm.cross.platform.steamobj.log.LogConfigStream;

/**
 * 早前这个log类仅仅简单的写文件操作 后面因为考虑要上报到服务器来。所以现在操作要支持本地文件和上报服务
 * */

public class FiLog {
	public static final int DEBUG_LEVEL_VERBOSE = 2;
	public static final int DEBUG_LEVEL_DEBUG = 3;
	public static final int DEBUG_LEVEL_INFO = 4;
	public static final int DEBUG_LEVEL_WARN = 5;
	public static final int DEBUG_LEVEL_ERROR = 6;
	public static final int DEBUG_LEVEL_ASSERT = 7;
	public final static String FILOG_TAG = "FiDebugInfo";

	protected static String DroidLogPath = "/data/local/tmp/vmtools/SysDaemonLog"; // android的目录
	protected static String WinLogPath = "E:/Project/SpVm/src/TestDir/log.txt"; // win的测试目录

	// 考虑log要实时打回服务器。有一些复杂的配置，所以要放在服务器
	protected static LogConfigStream DebugLogConfig = LogConfigStream.InitSysLogConfig;
	protected static ILogger Logout;

	/**
	 * envName参数指的是sys 环境或者是zygote环境或者是app pkg name的环境_
	 * 
	 * */

	public static void setStaticInstance(LogConfigStream logConfig,
			ILogger logout) {
		setLogConfig(logConfig);
		Logout = logout;
	}

	private static void setLogout(ILogger logout) {
		Logout = logout;
	}

	/**
	 * 设置log配置文件，可以达到所有的配置 所有的外围设干脆直接放这里算了
	 * */
	private static void setLogConfig(LogConfigStream logConfig) {
		if (logConfig == null) {
			return;
		}
		DebugLogConfig = logConfig;
	}

	public static void setDebug(boolean isDebug) {
		DebugLogConfig.setDebug(isDebug);
	}

	// 必须打印
	public static void Log(String tag, int level, String msg) {
		if (DebugLogConfig == null) {
			LogStdOut(tag, level, msg);
			return;
		}
		if (!DebugLogConfig.isDebug()) {
			return;
		}
		if (level < DebugLogConfig.getDebugLevel()) {
			return;
		}

		int envType = DebugLogConfig.getLogEnvironmentType();
		switch (envType) {
		case LogConfigStream.LOG_ADB:
		case LogConfigStream.LOG_TO_SERVER:
		case LogConfigStream.LOG_Native_FILE:
			if (Logout == null) {
				LogStdOut(tag, level, msg);
				return;
			}
			Logout.log(level, tag, msg);
			return;
		}
		LogStdOut(tag, level, msg);
	}

	public static void LogStdOut(String tag, int level, String msg) {
		System.out.println(formatLog(tag, level, msg));
	}

	public static String formatLog(String tag, int level, String msg) {
		return String.format("%s/%s: %s", levelToTag(level), tag, msg);
	}

	public static String levelToTag(int level) {
		switch (level) {
		case DEBUG_LEVEL_VERBOSE:
			return "v";
		case DEBUG_LEVEL_DEBUG:
			return "d";
		case DEBUG_LEVEL_INFO:
			return "i";
		case DEBUG_LEVEL_WARN:
			return "w";
		case DEBUG_LEVEL_ERROR:
			return "e";
		case DEBUG_LEVEL_ASSERT:
			return "a";
		default:
			return "n";
		}
	}

	public static void appNativeLog(String tag, String msg) {
		if (DebugLogConfig.getLogEnvironmentType() == LogConfigStream.LOG_Native_FILE) {
			Logout.log(DEBUG_LEVEL_ERROR, tag, msg);
			return;
		}
		LogStdOut(tag, DEBUG_LEVEL_ERROR, msg);
	}

	public static void appNativeLog(String msg) {
		appNativeLog(FILOG_TAG, msg);
	}

	public static void appLogV(String tag, String msg) {
		Log(tag, DEBUG_LEVEL_VERBOSE, msg);
	}

	public static void appLogD(String tag, String msg) {
		Log(tag, DEBUG_LEVEL_DEBUG, msg);
	}

	public static void appLogI(String tag, String msg) {
		Log(tag, DEBUG_LEVEL_INFO, msg);
	}

	public static void appLogW(String tag, String msg) {
		Log(tag, DEBUG_LEVEL_WARN, msg);
	}

	public static void appLogE(String tag, String msg) {
		Log(tag, DEBUG_LEVEL_ERROR, msg);
	}

	public static void appLogA(String tag, String msg) {
		Log(tag, DEBUG_LEVEL_ASSERT, msg);
	}

	public static void appLogV(String msg) {
		appLogV(FILOG_TAG, msg);
	}

	public static void appLogD(String msg) {
		appLogD(FILOG_TAG, msg);
	}

	public static void appLogI(String msg) {
		appLogI(FILOG_TAG, msg);
	}

	public static void appLogW(String msg) {
		appLogW(FILOG_TAG, msg);
	}

	public static void appLogE(String msg) {
		appLogE(FILOG_TAG, msg);
	}

	public static void appLogA(String msg) {
		appLogA(FILOG_TAG, msg);
	}

}
